home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cmouse / scribble.c < prev    next >
Text File  |  1988-05-02  |  6KB  |  170 lines

  1. /* SCRIBBLE.C:  Simple utility for drawing with mouse in graphics */
  2.  
  3. /* INCLUDES */
  4. #include <stdio.h>
  5. #include <dos.h>
  6. #include <graphics.h>
  7. #include <mouse.i>
  8. #include <gmouscur.i>
  9.  
  10. /* DEFINE TRUE/FALSE */
  11. #ifndef TRUE
  12. #define TRUE  -1
  13. #define FALSE  0
  14. #endif
  15.  
  16. /* DEFINE CONSTANTS */
  17. #define eventMask 0x55      /* any button released, or mouse moved */
  18. #define menu        20                      /* bottom of menu area */
  19. #define helpTop    185                         /* top of help area */
  20. #define box1       160               /* right end of each menu box */
  21. #define box2       320
  22. #define box3       480
  23. #define helpMsg1   "Click any button on Scribble to begin drawing"
  24. #define helpMsg2   "Hold down left button to draw"
  25.  
  26. /* GLOBALS */
  27. resetRec  *theMouse;
  28. locRec    *mouses;
  29.  
  30. /* LOCAL FUNCTIONS */
  31. void menuBox (int, int, char*);
  32. void help (char*);
  33. int  setUpScreen (void);
  34. void updateMeter (int, int);
  35. void work (void);
  36.  
  37. /* --------------------------------------------------------------- */
  38.  
  39. void main ()
  40. {
  41.   theEvents = MK_FP (_psp, 0x00C0);       /* point to event buffer */
  42.   initGCurs ();                        /* initialize cursor images */
  43.     theMouse = mReset ();                      /* initialize mouse */
  44.   if (theMouse->exists) {
  45.     theEvents->flag = 0;
  46.       if (setUpScreen()) {               /* if in graphics mode... */
  47.         mInstTask (eventMask, FP_SEG(handler),  /* install handler */
  48.                   FP_OFF(handler));
  49.         mGraphCursor (hand.hotX, hand.hotY, _DS,  /* graphics curs */
  50.                      (unsigned) hand.image);
  51.         mShow ();                                     /* cursor on */
  52.         mouses = mPos ();                          /* get position */
  53.         updateMeter (mouses->column, mouses->row);
  54.         work ();                      /* do mouse stuff until thru */
  55.         theMouse = mReset ();                       /* reset mouse */
  56.         closegraph ();                        /* back to text mode */
  57.       } else
  58.         puts ("\nGraphics mode not available. Program ended.");
  59.   } else
  60.     puts ("Mouse not active. Program ended.");
  61. } /* ------------------------ */
  62.  
  63. void menuBox (int x1, int x2, char *item)
  64.  
  65.     /* create a menu box between x1 and x2 at top of screen */
  66.  
  67. {
  68.     setviewport (x1+1, 1, x2-1, menu-1, FALSE);
  69.     clearviewport ();
  70.     outtextxy (50, 7, item);                       /* display text */
  71.     setviewport (0, 0, getmaxx(), helpTop, FALSE); /* drawing area */
  72. } /* ------------------------ */
  73.  
  74. void help (char *message)
  75.  
  76.     /* Display help message at bottom of screen */
  77.  
  78. {
  79.     int x;
  80.  
  81.     x = (getmaxx() - textwidth(message)) / 2;     /* For centering */
  82.     setviewport (1, helpTop+1, 638, getmaxy() - 1, FALSE);
  83.     clearviewport ();
  84.     outtextxy (x, 3, message);               /* write help message */
  85.     setviewport (0, menu, 639, helpTop, FALSE);    /* drawing area */
  86. } /* ------------------------ */
  87.  
  88. int setUpScreen (void)
  89.  
  90.     /* Prepare screen, return TRUE if done, FALSE if can't */
  91.  
  92. {
  93. int   driver   = CGA, mode = CGAHI;         /* use CGA hi-res mode */
  94. char  path [6] = "C:\TC";                       /* path to drivers */
  95.  
  96.   initgraph (&driver, &mode, path);              /* set graph mode */
  97.     if (graphresult () == grOk) {              /* if successful... */
  98.       setcolor (1);                                  /* initialize */
  99.       settextstyle (DEFAULT_FONT, HORIZ_DIR, 1);
  100.       menuBox (0, box1, "Scribble");            /* make menu boxes */
  101.       rectangle (0, 0, box1, menu);
  102.       menuBox (box1, box2, " Clear");
  103.       rectangle (box1, 0, box2, menu);
  104.       menuBox (box2, box3, "  Quit");
  105.       rectangle (box2, 0, box3, menu);
  106.       rectangle (box3, 0, 639, menu);             /* box for meter */
  107.       rectangle (0, helpTop, 639, getmaxy());      /* box for help */
  108.       help (helpMsg1);                     /* initial help message */
  109.       return (TRUE);                            /* successful exit */
  110.     } else
  111.       return (FALSE);                              /* unsuccessful */
  112. } /* ------------------------ */
  113.  
  114. void updateMeter (int x, int y)
  115.  
  116.     /* Update mouse position meter, upper right corner */
  117.  
  118. {
  119. char   position [9];
  120.  
  121.   sprintf (position, "%3d, %3d", x, y);   /* convert pos to string */
  122.   menuBox (box3, 639, position);                     /* display it */
  123. } /* ------------------------ */
  124.  
  125. void work (void)
  126.  
  127.      /* Draw with mouse until user clicks on Quit selection */
  128.  
  129. {
  130. int    thru = FALSE;
  131.  
  132.   do {
  133.     theEvents->flag = 0;                 /* clear mouse event flag */
  134.     while (theEvents->flag == 0);          /* wait for mouse event */
  135.     switch (theEvents->flag) {
  136.       case 0x0001:                              /* mouse has moved */
  137.         if ((theEvents->row > menu) &&
  138.             (theEvents->row < helpTop))            /* in work area */
  139.           if (theEvents->button == 1) {           /* and left down */
  140.             mHide ();
  141.             putpixel (theEvents->col, theEvents->row, 1);  /* draw */
  142.             mShow ();
  143.           }
  144.         updateMeter (theEvents->col, theEvents->row);    /* update */
  145.       break;
  146.       case 0x0004:
  147.       case 0x0010:
  148.       case 0x0040:                          /* any button released */
  149.         if (theEvents->row < menu)              /* if in menu area */
  150.           if (theEvents->col < box1) {                /* Scribble? */
  151.               mGraphCursor (cross.hotX, cross.hotY, _DS,
  152.                            (unsigned) cross.image);
  153.               help (helpMsg2);
  154.            }
  155.          else
  156.            if (theEvents->col < box2) {                  /* Clear? */
  157.              mHide ();
  158.              setviewport (0, menu+1, getmaxx(), helpTop-1, TRUE);
  159.              clearviewport ();
  160.              mShow ();
  161.              mGraphCursor (hand.hotX, hand.hotY, _DS,
  162.                           (unsigned) hand.image);
  163.              help (helpMsg1);
  164.            } else
  165.              if (theEvents->col < box3)                   /* Quit? */
  166.                thru = TRUE;
  167.           break;
  168.     }
  169.   } while (!thru);
  170. } /* ------------------------ */